import QQMapWX from "../libs/qqmap-wx-jssdk.js";
import {MAP_KEY} from "../config/config.ts"

export function getLocationFunc(locationData?: any) {
    return new Promise(async (resolve, reject) => {
        let res = {
            latitude: null,
            longitude: null
        }
        if (locationData?.cityName === '') {
            res.latitude = locationData.latitude
            res.longitude = locationData.longitude
        } else {
            res = await uni.getLocation({
                type: 'wgs84',
            });
        }
        const qqmapsdk = new QQMapWX({
            key: MAP_KEY
        });
        qqmapsdk.reverseGeocoder({
            location: {
                latitude: res.latitude,
                longitude: res.longitude
            },
            success({
                        result
                    }) {
                // console.log(result, "| result");
                if (locationData?.cityName !== '') {
                    locationData.name = result?.address_component?.city
                    locationData.latitude = res.latitude
                    locationData.longitude = res.longitude
                    uni.setStorageSync('locationData', {
                        ...locationData
                    })
                } else {
                    locationData.cityName = result?.address_component?.nation
                }
                resolve(locationData)
            },
            fail(error) {
                reject(error)
            }
        })
    })
}